/*
 * Copyright (c) 2006, 2022, webrx.cn All rights reserved.
 *
 */
package cn.webrx.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * <p>Project: smvc2022 - IndexController
 * <p>Powered by webrx On 2022-03-19 09:07:22
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
@Controller @RequestMapping("index")
// index  相当于  /index
public class IndexController {
    //a1 相当于 /index/a1  @ResponseBody不使用视图层显示，直接输出
    //相当于response.getWriter() out.print();
    @RequestMapping("a1") @ResponseBody
    public String a1() {
        return "hello";
    }

    @RequestMapping("a2") //hello.jsp
    public String a2(Model m) {
        m.addAttribute("msg","显示成功");
        return "index/hello";//WEB-INF/templates/index/hello.jsp 视图显示
    }

    //WEB-INF/templates/hello.jsp 显示
    @GetMapping({"/hello","/a3"})  //WEB-INF/templates/a3.jsp 显示
    public void hello(){
    }

    @GetMapping({"/a4","/a5"})
    public String a4(Model m){
        m.addAttribute("msg","a4+a5数据合并");
        return "index/hello";//WEB-INF/templates/index/hello.jsp 视图显示
    }

    //@GetMapping  get
    //@PostMapping post
    //@PutMapping put
    //@DeleteMapping delete
    //@PatchMapping patch
    @RequestMapping(value = "/a6",method = {RequestMethod.GET,RequestMethod.POST})
    public String a6(Model m){
        m.addAttribute("msg","a6");
        return "index/hello";//WEB-INF/templates/index/hello.jsp 视图显示
    }

    @GetMapping("/a7")
    public void a7(PrintWriter out){
        out.print("hello 中文效果");
    }

    @GetMapping("/a8")
    public void a8(HttpServletResponse resp) throws IOException {
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();
        out.println("<!DOCTYPE html>");
        out.println("<html>");
        out.println("<head>");
        out.println("<meta charset=\"utf-8\">");
        out.println("<title>玉灵 QQ:7031633 Email:webrx@126.com</title>");
        out.println("<meta name=\"keywords\" content=\"关键字\">");
        out.println("<meta name=\"description\" content=\"简介\">");
        out.println("</head>");
        out.println("<body>");

        out.println("<h3>Hello World  中文效果</h3>");

        out.println("</body>");
        out.println("</html>");
        out.flush();
        out.close();
    }

    @GetMapping("/a9")
    public void a9(HttpServletRequest req, HttpServletResponse resp, HttpSession session) throws IOException {
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();
        out.println("<!DOCTYPE html>");
        out.println("<html>");
        out.println("<head>");
        out.println("<meta charset=\"utf-8\">");
        out.println("<title>玉灵 QQ:7031633 Email:webrx@126.com</title>");
        out.println("<meta name=\"keywords\" content=\"关键字\">");
        out.println("<meta name=\"description\" content=\"简介\">");
        out.println("</head>");
        out.println("<body>");

        out.println("<h3>Hello World  中文效果</h3>");
        out.println(session.getId());

        out.println("</body>");
        out.println("</html>");
        out.flush();
        out.close();
    }

}
